Imports System.Data

Partial Class NewsControl
    Inherits System.Web.UI.UserControl

    Private _itemsToShow As Integer = 5
    Public Property ItemsToShow() As Integer
        Get
            Return _itemsToShow
        End Get
        Set(ByVal value As Integer)
            _itemsToShow = value
        End Set
    End Property

    Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Handles Me.Load


    End Sub

    Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

        ' modify the select statement so that only the required number of items are shown, and
        ' then only those that are older than today's date. This allows the database to be pre-filled
        ' with news items, and they'll only show when that date arrives.
        Dim sel As String = String.Format("SELECT Top {0} * FROM [News] WHERE DateToShow <= GETDATE() ORDER BY DateToShow DESC", _itemsToShow)

        SqlDataSource1.SelectCommand = sel

    End Sub

    Sub NewsItem_DataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)


        Dim row As DataRowView
        Dim img As Image

        ' if there's no picture associated with this news item, then hide the image
        ' this stops the 'missing image' icon being shown
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

            row = CType(e.Item.DataItem, DataRowView)

            If row("PictureUrl").ToString().Trim() = "" Then
                img = CType(e.Item.FindControl("NewsImage"), Image)
                img.Visible = False
            End If

        End If

    End Sub
End Class
